package cn.shop.dys.util;

import android.util.Log;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import cn.shop.dys.bean.UserInfo;
import cn.shop.dys.bean.MyResponseInfo;

/**
 * 请求处理工具
 * Created by jian on 2016/4/18.
 */
public class RequstUtil {
    private  HttpUtils mHttpUtils;
    private static RequstUtil mRequstUtil;
    public static final String HOST = "http://localhost/market";
    private final Gson mGson;

    private RequstUtil(){
        mHttpUtils = new HttpUtils();
        mGson = new Gson();

    }
    public static RequstUtil createInstance(){
        if(mRequstUtil==null){
            return new RequstUtil();
        }else {
            return mRequstUtil;
        }
    }
    public UserInfo regiter(UserInfo userInfo) {
        final UserInfo[] myUserInfo = {null};
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("username", userInfo.getUsername());
        params.addQueryStringParameter("password", userInfo.getPassword());
        mHttpUtils.send(HttpRequest.HttpMethod.GET, HOST + "/register", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            MyResponseInfo myResponseInfo = mGson.fromJson(result, MyResponseInfo.class);
                            if (myResponseInfo.getResponse().equals("register")) {
                                if (myResponseInfo.getUserInfo() != null) {
                                    Log.d("test", "info" + myResponseInfo.getUserInfo());
                                    myUserInfo[0] = myResponseInfo.getUserInfo();
                                }
                            }
                        } else {

                            //       mTextView.setText("返回的错误码：" + statusCode);
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                        //      mTextView.setText(msg);
                    }
                });
        Log.d("test", "info1" + myUserInfo[0]);
        return myUserInfo[0];
    }

    public  String login(UserInfo userInfo) {
        final String[] loginStr = {null};
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        RequestParams params = new RequestParams();
        //针对HttpMethod.GET请求
        params.addQueryStringParameter("username", userInfo.getUsername());
        params.addQueryStringParameter("password", userInfo.getPassword());
        //针对HttpMethod.POST请求
	/*	params.addBodyParameter("username", "test");
		params.addBodyParameter("password", "test");*/

        // params.addBodyParameter("pId", "1");
        mHttpUtils.send(HttpRequest.HttpMethod.GET, HOST + "/login", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            loginStr[0] = result;
                       //     mTextView.setText(result);
                        //    Toast.makeText(MainActivity.this, result, 1).show();

                        } else {

                        //    mTextView.setText("返回的错误码：" + statusCode);
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    //    mTextView.setText(msg);
                    }
                });
        return loginStr[0];
    }
    public  String logout(UserInfo userInfo) {
        final String[] logoutStr = {null};
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        RequestParams params = new RequestParams();

        params.addHeader("userid", userInfo.getUserid());
        // params.addBodyParameter("pId", "1");
        mHttpUtils.send(HttpRequest.HttpMethod.GET, HOST + "/logout", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            logoutStr[0]=result;
                       //     mTextView.setText(result);
                        //    Toast.makeText(MainActivity.this, result, 1).show();

                        } else {

                       //     mTextView.setText("返回的错误码：" + statusCode);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    //    mTextView.setText(msg);
                    }
                });
        return logoutStr[0];
    }
   /* public void getAddressList(View view) {

        mHttpUtils.configDefaultHttpCacheExpiry(0);
        mHttpUtils.configTimeout(10000);
        RequestParams params = new RequestParams();
        params.addHeader("userid", "20428");
        mHttpUtils.send(HttpRequest.HttpMethod.POST, HOST + "/addresslist", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(MyResponseInfo<String> responseInfo) {

                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                      //      mTextView.setText(result);
                       //     Toast.makeText(MainActivity.this, result, 1).show();
                        //    Log.i("itcast", result);

                        } else {

                      //      mTextView.setText("返回的错误码：" + statusCode);
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    //    mTextView.setText(msg);
                    }

                });

    }
    public void saveAddress(View view) {

        mHttpUtils.configDefaultHttpCacheExpiry(0);
        mHttpUtils.configTimeout(10000);
        RequestParams params = new RequestParams();
        params.addHeader("userid", "20428");
        params.addBodyParameter("name", "肖文");
        params.addBodyParameter("phoneNumber", "15801477821");
        params.addBodyParameter("province", "湖北");
        params.addBodyParameter("city", "武汉");
        params.addBodyParameter("addressArea", "洪山区");
        params.addBodyParameter("addressDetail", "街道口");

        // params.addBodyParameter("name", *//*"肖文"*//*"xiaowen");
        // params.addBodyParameter("phoneNumber", "15801477821");
        // params.addBodyParameter("province", *//*"湖北"*//*"hubei");
        // params.addBodyParameter("city", *//*"武汉"*//*"wuhan");
        // params.addBodyParameter("addressArea", *//*"洪山区"*//*"honsan");
        // params.addBodyParameter("addressDetail", *//*"街道口"*//*"jiedaokou");

        params.addBodyParameter("zipCode", "430070");
        params.addBodyParameter("isDefault", "1");

        mHttpUtils.send(HttpRequest.HttpMethod.POST, HOST + "/addresssave", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(MyResponseInfo<String> responseInfo) {

                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                      //      mTextView.setText(result);
                       //     Toast.makeText(MainActivity.this, result, 1).show();
                       //     Log.i("itcast", result);

                        } else {

                       //     mTextView.setText("返回的错误码：" + statusCode);
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                  //      mTextView.setText(msg);
                    }

                });

    }*/
    public  String getUserInfo(UserInfo userInfo) {
        final String[] userInfoStr = {null};
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        mHttpUtils.configTimeout(10000);
        RequestParams params = new RequestParams();

        //添加请求头
        params.addHeader("userid", userInfo.getUserid());


        //	params.addBodyParameter("sku", "1:3:1,3|2:5:1,4");


        // params.addBodyParameter("sku", "1:3:1,2,3,4|2:2:2,3");
        // params.addBodyParameter("addressId", "139");
        // params.addBodyParameter("paymentType", "1");
        // params.addBodyParameter("deliveryType", "1");
        // params.addBodyParameter("invoiceType", "2");
        // params.addBodyParameter("invoiceTitle", "传智慧播客教育科技有限公司");
        // params.addBodyParameter("invoiceContent", "传智慧播客教育科技有限公司");

        // params.addBodyParameter("page", "0");
        // params.addBodyParameter("pageNum", "10");


        // params.addBodyParameter("type", "2");
        // params.addBodyParameter("page", "0");
        // params.addBodyParameter("pageNum", "10");

        // params.addBodyParameter("pId", "1");
        mHttpUtils.send(HttpRequest.HttpMethod.POST, HOST + "/userinfo", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            userInfoStr[0]=result;
                      //      mTextView.setText(result);
                       //     Toast.makeText(MainActivity.this, result, 1).show();
                        //    Log.i("itcast", result);

                        } else {

                         //   mTextView.setText("返回的错误码：" + statusCode);
                        }

                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                     //   mTextView.setText(msg);
                    }

                });
        return userInfoStr[0];
    }
}
